<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/2/9
  Time: 12:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/materialdesignicons.min.css" rel="stylesheet">
    <link href="/css/style.min.css" rel="stylesheet">
    <link href="/layui/css/layui.css" rel="stylesheet">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script type="text/javascript" src="/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/js/main.min.js"></script>
    <script type="text/javascript" src="/layui/layui.js"></script>
</head>
<body>
<div class="container-fluid p-t-15">
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-toolbar clearfix">
                    <div class="toolbar-btn-action">
                        <a id="addTeacherButtons" class="btn btn-primary m-r-5" ><i class="mdi mdi-plus"></i> 新增</a>
                    </div>
                    <table class="table table-bordered" id="teacherList" lay-filter="table1"></table>
                    <div id="page1"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<%--添加模板--%>
<div id="addTeacherView" style="display: none">
    <form>
        <div class="form-group">
            <label>姓名</label>
            <input class="form-control" type="text" id="teacherName" name="teacherName" >
        </div>
        <div class="form-group">
            <label>员工密码</label>
            <input class="form-control" type="text" id="teacherPassword" name="teacherPassword" >
        </div>
        <div class="form-group">
            <label>员工电话</label>
            <input class="form-control" type="text" id="teacherPhone" name="teacherPhone" >
        </div>
        <div class="form-group">
            <button class="btn btn-primary" type="submit" id="addTeacherButton">添加</button>
        </div>
    </form>
</div>
<%--修改模板--%>
<div id="updateTeacherView" style="display: none">
    <form>
        <div class="form-group">
            <label>员工工号</label>
            <input class="form-control" type="text" id="teacherNo1" name="teacherNo1" readonly="true">
        </div>
        <div class="form-group">
            <label>员工姓名</label>
            <input class="form-control" type="text" id="teacherName1" name="teacherName" >
        </div>
        <div class="form-group">
            <label>员工密码</label>
            <input class="form-control" type="text" id="teacherPassword1" name="teacherPassword" >
        </div>
        <div class="form-group">
            <label>员工电话</label>
            <input class="form-control" type="text" id="teacherPhone1" name="teacherPhone" >
        </div>
        <div class="form-group">
            <button class="btn btn-primary" type="submit" id="updateTeacherButton">修改</button>
        </div>
    </form>
</div>
<script type="text/html" id="toolbar1">
    <div class="btn-group">
        <button class="btn btn-success m-r-5" lay-event="update">编辑</button>
        <button class="btn btn-danger" lay-event="delete">删除</button>
    </div>
</script>
<script type="text/javascript">
    layui.use(["table","layer"],function () {
        var table=layui.table;
        var layer=layui.layer;
        var form=layui.form;
        table.render({
            elem:'#teacherList',
            skin:'row,line',
            method:"post",
            url:'/teacher/allteacher.action',
            where:{type:'teacherMess'},
            limit:5,
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']//自定义分页布局
                ,limits:[5,10,15]
                ,first: '首页' //不显示首页
                ,last:"尾页" //不显示尾页
            },
            skin:'nob',// 无边框风格
            size:'lg',// 大尺寸
            done:function () {
                $(".layui-table-box").css("border-width","1px");
                $(".layui-table-header tr").css("height","50px");
                $(".layui-table-header tr td").css("width","50px");
                $(".layui-table-header tr").css("background-color","white");
                $(".layui-table-header tr span").css("color","#666666");
                $(".layui-table-body tr").css("height","50px");
            },
            cols:[[
                {field:'teacherNo',title:'员工工号',sort:true,align:'center',width:140},
                {field:'teacherName',title:'员工姓名',align:'center',width:200},
                {field:'password',title:'员工密码',sort:true,align:'center',width:200,templet:function (d) {
                        return '<p>******</p>'
                    }},
                {field:'teacherPhone',title:'员工电话',sort:true,align:'center',width:160},
                {title:'操作',toolbar:"#toolbar1",align:'center'}
            ]]

        });
        table.on('tool(table1)', function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if(layEvent === 'delete'){ //删除
                layer.confirm('真的删除此员工吗', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    //向服务端发送删除指令
                    $.ajax({
                        type:"post",
                        url:"/teacher/deleteTeacher.action",
                        data:{teacherNo:data.teacherNo},
                        dataType:"json",
                        success:function (message) {
                            layer.msg(message)
                        }
                    })
                });
            } else if(layEvent === 'update') { //编辑
                $("#teacherNo1").val(data.teacherNo);
                $("#teacherName1").val(data.teacherName);
                $("#teacherPassword1").val(data.password);
                $("#teacherPhone1").val(data.teacherPhone);
                //do something
                layer.open({
                    type:1,
                    title:"更新员工信息",
                    area: ['400px', '400px'],
                    content:$("#updateTeacherView")
                })
            }
        });

        $("#updateTeacherButton").click(function () {
            $.ajax({
                type:"post",
                url:"/teacher/updateTeacher.action",
                data:{teacherNo:$("#teacherNo1").val(),teacherName:$("#teacherName1").val(),password:$("#teacherPassword1").val(),teacherPhone:$("#teacherPhone1").val()},
                dataType:"json",
                success:function (message) {
                    if (message.code==1){
                        layer.msg("修改成功");
                    }else{
                        layer.msg("修改失败");
                    }
                }
            })
        });
        $("#addTeacherButtons").click(function () {
            layer.open({
                type:1,
                title:"添加员工信息",
                area: ['400px', '500px'],
                content:$("#addTeacherView")
            })
        });
        $("#addTeacherButton").click(function () {
            $.ajax({
                type:"post",
                url:"/teacher/addTeacher.action",
                data:{teacherName:$("#teacherName").val(),password:$("#teacherPassword").val(),teacherPhone:$("#teacherPhone").val()},
                dataType:"json",
                success:function (message) {
                    if (message.code==0){
                        layer.msg("添加失败");
                    }else{
                        layer.msg("添加成功");
                    }
                }
            })
        });
    })

</script>
</body>
</html>
